import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_module/system/colors_config.dart';
import 'package:fluttertoast/fluttertoast.dart';

/**
 * 低图自定义图层 dialog
 */
typedef MyLayerDialogCallBack = Function(
    String oldName, String newName, String url);

class MyLayerDialog extends StatelessWidget {
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _urlController = TextEditingController();

  final String name;
  final String url;
  final Key? key;
  final MyLayerDialogCallBack callBack;

  MyLayerDialog(
      {this.name: "", this.url: "", this.key, required this.callBack}) {
    _nameController.text = name;
    _urlController.text = url;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Card(
        elevation: 2,
        margin: const EdgeInsets.only(top: 80, left: 10, right: 10),
        color: MyColors.secondBlackFillColor,
        shadowColor: MyColors.textWhiteColor,
        child: Padding(
          padding: const EdgeInsets.only(top: 20, bottom: 30, left: 15, right: 15),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text(
                "添加地图快捷方式",
                style: TextStyle(color: MyColors.textBlueColor, fontSize: 22),
              ),
              const SizedBox(
                height: 10,
              ),
              Row(
                children: [
                  const Text(
                    "名称：",
                    style:
                        TextStyle(color: MyColors.textWhiteColor, fontSize: 20),
                  ),
                  Flexible(
                    child: TextField(
                      controller: _nameController,
                      maxLines: 1,
                      style: const TextStyle(
                          color: MyColors.textWhiteColor, fontSize: 18),
                      decoration: InputDecoration(
                        hintText: "自定义名称",
                        hintStyle: const TextStyle(
                            color: MyColors.mainGrayColor, fontSize: 18),
                        enabledBorder: const UnderlineInputBorder(
                          // 不是焦点的时候颜色
                          borderSide: BorderSide(color: MyColors.mainGrayColor),
                        ),
                        focusedBorder:  const UnderlineInputBorder(
                          // 焦点集中的时候颜色
                          borderSide: BorderSide(color: MyColors.textBlueColor),
                        ),
                        suffixIcon: IconButton(
                            onPressed: () => _nameController.text = "",
                            icon: const Icon(
                              Icons.cancel_outlined,
                              color: MyColors.textWhiteColor,
                            )),
                      ),
                    ),
                  )
                ],
              ),
              const SizedBox(
                height: 10,
              ),
              Row(
                children: [
                  const Text(
                    "地址：",
                    style:
                        TextStyle(color: MyColors.textWhiteColor, fontSize: 20),
                  ),
                  Flexible(
                    child: TextField(
                      controller: _urlController,
                      maxLines: 1,
                      style: const TextStyle(
                          color: MyColors.textWhiteColor, fontSize: 18),
                      decoration: InputDecoration(
                        hintText: "请输入url地址",
                        hintStyle: const TextStyle(
                            color: MyColors.mainGrayColor, fontSize: 18),
                        enabledBorder: const UnderlineInputBorder(
                          // 不是焦点的时候颜色
                          borderSide: BorderSide(color: MyColors.mainGrayColor),
                        ),
                        focusedBorder:  const UnderlineInputBorder(
                          // 焦点集中的时候颜色
                          borderSide: BorderSide(color: MyColors.textBlueColor),
                        ),
                        suffixIcon: IconButton(
                            onPressed: () => _urlController.text = "",
                            icon: Icon(
                              Icons.cancel_outlined,
                              color: MyColors.textWhiteColor,
                            )),
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(
                height: 30,
              ),
              Row(
                children: [
                  Flexible(
                    child: Padding(
                      padding: const EdgeInsets.only(right: 10),
                      child: CupertinoButton(
                        padding: const EdgeInsets.only(
                            left: 10, right: 10, top: 5, bottom: 5),
                        child: Container(
                          width: double.infinity,
                          child: const Text(
                            '取消',
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 20,
                            ),
                            maxLines: 1,
                          ),
                        ),
                        onPressed: () {
                          Navigator.maybePop(context);
                        },
                        color: MyColors.mainGrayColor,
                      ),
                    ),
                  ),
                  Flexible(
                    child: Padding(
                      padding: const EdgeInsets.only(left: 10),
                      child: CupertinoButton(
                        padding: const EdgeInsets.only(
                            left: 10, right: 10, top: 5, bottom: 5),
                        child: Container(
                          width: double.infinity,
                          child: const Text(
                            '确定',
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              color: MyColors.mainBlackColor,
                              fontSize: 20,
                            ),
                            maxLines: 1,
                          ),
                        ),
                        onPressed: () {
                          if (_nameController.text == "") {
                            Fluttertoast.showToast(msg: "请输入名称");
                            return;
                          }
                          if (_urlController.text == "") {
                            Fluttertoast.showToast(msg: "请输入地址");
                            return;
                          }

                          callBack(
                              name, _nameController.text, _urlController.text);
                        },
                        color: MyColors.textBlueColor,
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
